/*
 * GetOrders.java
 *
 * Created on 11 janvier 2008, 00:34
 */
package com.reimscopie.console ;

import javax.naming.* ;
import javax.transaction.* ;

import com.reimscopie.* ;
import com.reimscopie.beans.interfaces.* ;


/**
 * Display all orders.
 */
class GetOrders extends BeanCommand {
  
  /**
   * Create a new instance of <code>GetOrders</code>.
   */
  GetOrders() {
    
    super("com.reimscopie.beans.session.ReimsCopieAdminBean",
        ReimsCopieAdminRemote.class) ;
    
  }
  
  
  /**
   * Return the name of this command. The name is the first word in the shell
   * command line.
   * 
   * @return The command name.
   */
  @Override
  public String getName() {
    return "get-orders" ;
  }
  

  /**
   * Return a short help message about this command.
   * 
   * @return The command help.
   */
  @Override
  public String getHelp() {
    return "Display submited orders" ;
  }
  
  
  /**
   * Return the command usage. This string must give all command options with
   * a description for each option.
   * 
   * @return The command usage.
   */
  @Override
  public String getUsage() {
    
    StringBuilder builder = new StringBuilder() ;
    
    builder.append("get-orders [--valid=valid]\n") ;
    builder.append("   vaild   Display only vaild orders\n") ;
    
    return builder.toString() ;
    
  }

  /**
   * Execute this command.
   * 
   * @param args The arguments given in the command line.
   * @param output The command output.
   *
   * @exception CommandException The command failed.
   */
  @Override
  public void execute(String[] args,ConsoleOutput output)
  throws CommandException {
    
    String option = getOption("valid",args) ;
    boolean valid = (option != null) ;
    
    
    try {
      // get JNDI initial context
      Context context = new InitialContext() ;
      UserTransaction transaction = getTransaction(context) ;
      ReimsCopieAdminRemote bean = (ReimsCopieAdminRemote)getBean(context) ;
      
      if (bean == null) {
        output.printError("error: no bean",true) ;
        return ;
      }
      
      try {
        transaction.begin() ;
        
        // get orders
        Order[] orders ;
        // ouput
        if (valid)
          orders = bean.getValidOrders() ;
        else
          orders  = bean.getOrders() ;
        
        // output
        output.print("Order count: "+orders.length,true) ;
        for (Order order : orders) {
          output.print('-') ;
          output.print(order.toString()) ;
          Purchase[] purchases = order.getPurchases() ;
          output.println() ;
          for (Purchase purchase : purchases) {
            output.print("   -") ;
            output.print(purchase.toString()) ;
            output.println() ;
          }
        }
        
        transaction.commit() ;
        output.print("Done",true) ;
      }
      catch (Exception e) {
        output.printError("Failed",true) ;
        try {
          transaction.rollback() ;
        }
        catch (SystemException ex) {
          output.printError(ex) ;
        }
        output.printError(e) ;
      }
      
    }
    catch (NamingException e) {
      output.printError("Cannot get initial context for JNDI",true) ;
      output.printError(e) ;
      return ;
    }
  }
  
}
